
var quicktuiDialog = null;	// quicktui dialog object

var quicktui = {
	onLoad: function() {
		// initialization code
		this.initialized = true;
		//this.strings = document.getElementById("quicktui-strings");
		
		// init the key config
		this.initKeyConfig();

		var scope = this;
		document.getElementById("contentAreaContextMenu")
		       .addEventListener("popupshowing", function(e) { scope.showContextMenu(e); }, false);
	},

	showContextMenu: function(event) {
		// show or hide the menuitem based on what the context menu is on
		// see http://kb.mozillazine.org/Adding_items_to_menus
		// document.getElementById("context-quicktui").hidden = gContextMenu.onImage;
		var menuitem = document.getElementById("quicktui-context-item");
		if (menuitem) {
			var prefContextMenu = QuickTuiUtils.prefs.getBoolPref("showContextMenu");
			
			menuitem.hidden = !prefContextMenu;	// && gContextMenu.isTextSelected);
		}
	},

	onMenuItemCommand: function(e) {
		this.openQuickTuiDialog(e);
	},
	
	onToolbarButtonCommand: function(e) {
		this.openQuickTuiDialog();
  	},

	openQuickTuiDialog: function(boolContent) {
		if(quicktuiDialog == null || quicktuiDialog.closed) {
			var winFeatures = "alwaysRaised, width=420, height=260";
			
			var prefLeft = QuickTuiUtils.prefs.getIntPref("dialogLeft");
			var prefTop = QuickTuiUtils.prefs.getIntPref("dialogTop");
			if (prefLeft == 0 || prefTop == 0) {
				winFeatures += ", centerscreen"
			} else {
				winFeatures += ", left=" + prefLeft + ", top=" + prefTop;
			}
			
  			quicktuiDialog = window.openDialog("chrome://quicktui/content/quicktuiDialog.xul", 
				"quicktuiDialog",
				winFeatures,
				boolContent);
		} else {
			quicktuiDialog.focus();
		}
	},
	
	openQuickTuiContentDialog: function() {
		this.openQuickTuiDialog(true);
	},
	
	openQuickTuiEmptyDialog: function() {
		this.openQuickTuiDialog(false);
	},
	
	initKeyConfig: function() {
		var hotkeyPrefs = ["hotkeyContentDialog", "hotkeyEmptyDialog"];
		
		for (var i=0; i < hotkeyPrefs.length; i++) {
			
			var keyPref = QuickTuiUtils.prefs.getCharPref(hotkeyPrefs[i]);
			var params = keyPref.split(/,/);

			var keyItem = document.getElementById("quicktui-key-" + hotkeyPrefs[i]);
			if (keyItem) {
				if (params[0]) 
					keyItem.setAttribute("key", params[0]);
				if (params[1]) 
					keyItem.setAttribute("keycode", params[1]);
				keyItem.setAttribute("modifiers", params[2]);
			}
		}
	}

};
window.addEventListener("load", function(e) { quicktui.onLoad(e); }, false);
